// 增删改查的请求地址
const urlPrefix = "/publishController"
const urlAdd = urlPrefix + "/add";
const urlDel = urlPrefix + "/delete";
const urlEdit = urlPrefix + "/update";
const urlQuery = urlPrefix + "/list";
const urlPublish = urlPrefix + "/publish";
let server = [];
const urlDropdownVersion = "/zjfUserModuleVersion/list";
const urlDropdownBaseServer = "/zjfBaseServerInfo/list";
const param = {"PAGE_CAPACITY": 1000, "PAGE_OFFSET": 0, "SORT_TYPE": "asc"};
let keyList = [];
let versionList = [];
customerClickToSelect = true;
const columns =
    [{
        checkbox: true
    }, {
        field: 'ID',
        title: 'ID',
        align: 'center'
    }, {
        field: 'NAME',
        title: '服务名称',
        align: 'center',
        sortable: true
    }, {
        field: 'PARENT',
        title: '父路径',
        align: 'center',
        sortable: true
    }, {
        field: 'USER',
        title: '用户',
        align: 'center',
        visible: true
    }, {
        field: 'VERSION',
        title: '版本',
        align: 'center',
        visible: true,
        sortable: true,
        editable: {
            disabled: true,
            type: 'select',
            title: '版本',
            source: function () {
                let list = [];
                for (it of versionList) {
                    list.push({value: it.VERSION, text: it.VERSION});
                }
                return list;
            }
        }
    }
    ];

// 初始化表格之前 初始化添加的下拉框,可以异步
function beforeInitTable() {
    $("#m_publish").addClass("active");

    showAttachedVersion();
}

function showAttachedVersion() {
    let jsonObj = auth_user;
    param["USER"] = jsonObj.USER;
    // 这里用同步,不然后面加载不到
    ajax(urlDropdownVersion, param, false, 2)
}


// 666
// K V形式的对象,自定义请求参数
function getCustomerParam() {
    let customer_param = [];
    let obj = {}
    obj.K = "USER";
    obj.V = auth_user.USER;
    customer_param.push(obj);
    return customer_param;
}


// 初始化下拉框
function customerInitBtn() {
    // $("#scan_btn").addClass("hidden");
    // $("#customer_select_btn").addClass("hidden");
    // $("#btn_delete").addClass("hidden");
    $("#btn_publish").removeClass("hidden");

    $("#btn_publish").click(function () {
        let rows = $('#list').bootstrapTable('getSelections');
        // 如果没有选中数据
        if (rows.length < 1) {
            errorMsg("没有选中任何记录", type.INFO)
            return;
        }
        $("#myModal").modal("show");
        scanDropdownBaseServer();
    });
}

// 模态框的确认事件
function modalConfirm() {
    /*
        // errorMsg("点击了发布按钮", type.INFO)
        let rows = $('#list').bootstrapTable('getSelections');
        // 如果没有选中数据
        if (rows.length < 1) {
            errorMsg("没有选中任何记录", type.INFO)
            return;
        }
        // 非阻塞确认框....
        publish(rows);
    */
    // 这个就是 SCAN_SERVER
    let formObj = getScanTableVal("scan_form");
    console.log("模态框确认事件")
    console.log(formObj);
    for (const obj in formObj) {
        if (formObj[obj] === null) {
            errorMsg("不能有空参", type.DANGER)
            return;
        }
    }

    let rows = $('#list').bootstrapTable('getSelections');
    // 如果没有选中数据
    if (rows.length < 1) {
        errorMsg("没有选中任何记录", type.INFO)
        return;
    }
    // 非阻塞确认框....
    publish(rows,formObj);
    // ajax(urlScanService, formObj, true, 5);
}

function scanDropdownBaseServer() {
    ajax(urlDropdownBaseServer, param, false, 4)
}

function publish(rows,formObj) {
    console.log("---------rows--------");
    console.log(rows);
    let ids = rows.map(r => {
        return r.ID
    })
    let param = {}
    param["IDS"] = ids;
    for (let item in formObj) {
        param[item] = formObj[item];
    }
    // 同步删除数据,然后刷新列表
    ajax(urlPublish, param, true, 3)
    errorMsg("请求发布成功,正在进行打包", type.INFO)
}
function customer_sheet_name() {
    return "发布列表";
}